package com.zhuangfei.hpuhelper.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.lzf.serviceplatform.R;

import com.hpu.bean.Course;
import com.hpu.constants.ShareConstants;
import com.hpu.parser.CourseParser;
import com.hpu.tools.ShareTools;
import com.zhuangfei.hpuhelper.tools.ActivityTools;
import com.zhuangfei.hpuhelper.tools.CourseTools;
import com.zhuangfei.toolkit.IntentToolkit;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;

public class NoTimeClassActivity extends Activity {

	/**
	 * 列表
	 */
	private List<Map<String, String>> items = new ArrayList<>();
	private ListView listView;
	private SimpleAdapter adapter;

	/**
	 * 返回
	 */
	private LinearLayout backLayout;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_no_time_class);
		ActivityTools.setTransparent(this);
		initView();
		initEvent();
		loadData();
	}

	private void loadData() {
		items.clear();
		String string2 = (String) ShareTools.get(this, ShareConstants.STORE_COURSE, "");
		CourseParser courseParser=new CourseParser();
		List<Course> courses=courseParser.parse(string2);
		if(courses!=null){
			courses= CourseTools.getNoTimeList(this,courses);
			for(int i=0;i<courses.size();i++){
				Map<String, String> map = new HashMap<>();
				Course course=courses.get(i);
				map.put("name",course.getName()==null?"Unknow":course.getName());
				map.put("teacher", course.getTeacher()==null?"Unknow":course.getTeacher());
				items.add(map);
			}
		}

		if(items.size()==0) Toast.makeText(this, "你还没有此类课程", Toast.LENGTH_SHORT).show();
		
	}

	private void initView() {
	
		backLayout = (LinearLayout) findViewById(R.id.id_back);
		listView = (ListView) findViewById(R.id.id_notime_listview);
		adapter = new SimpleAdapter(this, items, R.layout.item_notimecourse_layout, new String[] { "name","teacher" },
				new int[] { R.id.id_course_name, R.id.id_course_value });
		listView.setAdapter(adapter);
	}

	private void initEvent() {
		backLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				ActivityTools.toBackActivityAnim(NoTimeClassActivity.this, MenuActivity.class);
			}
		});

		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				Map<String,String> map=items.get(i);
				new IntentToolkit().with(NoTimeClassActivity.this)
						.target(AddCourseActivity.class)
						.put("name",map.get("name"))
						.put("teacher",map.get("teacher"))
						.execute();
			}
		});
	}

	@Override
	public void onBackPressed() {
		ActivityTools.toBackActivityAnim(this, MenuActivity.class);
	}
}
